/*
 * @Author: 米亚流年 miyaliunian@gmail.com
 * @Date: 2023-12-30 22:35:09
 * @LastEditors: 米亚流年 miyaliunian@gmail.com
 * @LastEditTime: 2024-03-26 15:41:57
 * @FilePath: /Component-Gallery/docs/demo/utils/route.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { h } from 'vue'
import { RouterLink } from 'vue-router'

import menus from './menus'

export interface IMenuOption {
  type?: 'group' | 'item'
  key?: string
  count?: number
  label: string
  path?: string
  enSuffix?: boolean
  children?: IMenuOption[]
}

export function findMenuValue(
  options: IMenuOption[],
  path: string
): string | undefined {
  for (const option of options) {
    if (option.children) {
      const value = findMenuValue(option.children, path)
      if (value) return value
    }
    if (option.path === path) {
      return option.key
    }
  }
  return undefined
}

export const renderMenuLabel = (option: IMenuOption) => {
  if (!('path' in option) || option.label === '--Debug') {
    return option.label
  }
  return h(
    RouterLink,
    {
      to: option.path
    },
    { default: () => option.label }
  )
}

const appendCounts = (item: IMenuOption) => {
  if (!item.children) {
    item.count = 1
    return item
  } else {
    item.children.forEach(appendCounts)
    item.count = item.children.reduce((sum, item) => sum + (item.count ?? 0), 0)
    if (item.type === 'group') {
      // item.label += ` (${item.count})`
      item.label
    }
    return item
  }
}

function createItems(prefix: string, items: IMenuOption[]) {
  return items.map((rawItem) => {
    const item = {
      ...rawItem,
      key: rawItem.label,
      label: rawItem.label,
      // extra: rawItem.enSuffix ? rawItem.label : undefined,
      path: rawItem.path ? prefix + rawItem.path : undefined
    }
    if (rawItem.children) {
      item.children = createItems(prefix, rawItem.children)
    }
    return item
  })
}

export function createComponentMenuOptions() {
  return createItems('/components', [
    appendCounts({
      label: '通用组件',
      type: 'group',
      children: menus
    })
  ])
}

export function createDocsMenuOptions() {
  return createItems('/introduction', [
    appendCounts({
      label: '说明文档',
      type: 'group',
      children: [
        {
          label: '概述',
          enSuffix: true,
          path: '/summary'
        },
        {
          label: '屏幕自适应',
          enSuffix: true,
          path: '/auto-rem'
        },
        {
          label: '定制主题',
          enSuffix: true,
          path: '/theme'
        },
        {
          label: '自定义组件CSS规范',
          enSuffix: true,
          path: '/customize_comp'
        },
        {
          label: '远程组件集成',
          enSuffix: true,
          path: '/remote'
        },
        {
          label: '组件API规范',
          enSuffix: true,
          path: '/comp-docs'
        },
        {
          label: '组件化地图操作手册',
          enSuffix: true,
          path: '/map-manual'
        }
      ]
    })
  ])
}
